|
|
|
|
|
|
|
something. To see how the Len function works, enter each of these lines in the Immediate window and note the different results: |
|
|
|
|
|
|
|
|
Print Len("Hello")
Print Len("") |
|
|
|
|
|
|
|
|
You may also need to check the returned value to make sure it is of the proper type. If you are expecting a number that will subsequently be compared to another number in an If statement, your program should present an error message if the user enters letters. To make sure that you have a numerical value with which to work, you can use the Val function. The Val function's purpose is to return the numerical value of a string. If the string contains or starts with numbers, the function returns the number. If the string does not start with a number, the function returns zero. To understand the Val function, enter these lines in the Immediate window to see what each returns: |
|
|
|
|
|
|
|
|
Print Val("Hello")
Print Val("50 ways to leave your lover")
Print Val("100 and 1 make 101") |
|
|
|
|
|
|
|
|
The following code illustrates additional processing of the returned value of the input box with Val and Len: |
|
|
|
|
|
|
|
|
Dim stInputVal As String
stInputVal = InputBox("Enter your age")
If Len(stInputVal) = 0 Then
MsgBox "No age was selected"
Else
If Val(stInputVal) = 0 Then
MsgBox "You entered an invalid age."
Else
MsgBox "Congratulations for surviving this long!"
End If
End If |
|
|
|
|
|
|
|
|
Using Built-In Dialog Boxes |
|
|
|
|
|
|
|
|
In earlier sections, you learned what a dialog box is and how to use two simple dialog boxes. In the following sections, you'll learn about the Microsoft CommonDialog control, which allows you to use standard Windows dialog boxes to specify filenames, select fonts and colors, and control the printer. And although the ease of setup is a great benefit, an even bigger bonus is that these dialog boxes are already familiar to the user because they are the same dialog boxes used by Windows itself. |
|
|
|
|
|
|
|
|
General Usage of the CommonDialog Control |
|
|
|
|
|
|
|
|
Using a single CommonDialog control, you have access to the following standard Windows dialog boxes: |
|
|
|
|
|
|
|
|
Open. Lets the user select the name and location of a file to open. |
|
|
|
|
|